package com.ctcemti.mdm.common.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

/**
 * 
 * ClassName: DateUtils   
 * @author GuanRuming Email:guanruming@youotech.com  
 * @date 2018年1月15日  
 * @Description: 日期工具类
 */
public class DateUtil {
	private static Set<String> dateFormats = new LinkedHashSet<String>();
	private static Map<Integer, String> monthMaps = new HashMap<Integer, String>();
	private static Map<Integer, String> weekdayMaps = new HashMap<Integer, String>();
	// 注册一下默认识别的日期转换格式
	static {
		DateUtil.dateFormats.add("yyyy-MM-dd HH:mm:ss");
		DateUtil.dateFormats.add("yyyy/MM/dd HH:mm:ss");
		DateUtil.dateFormats.add("yyyy-MM-dd HH:mm");
		DateUtil.dateFormats.add("yyyy-MM-dd");
		DateUtil.dateFormats.add("yyyy/MM/dd");

		DateUtil.monthMaps.put(1, "一月");
		DateUtil.monthMaps.put(2, "二月");
		DateUtil.monthMaps.put(3, "三月");
		DateUtil.monthMaps.put(4, "四月");
		DateUtil.monthMaps.put(5, "五月");
		DateUtil.monthMaps.put(6, "六月");
		DateUtil.monthMaps.put(7, "七月");
		DateUtil.monthMaps.put(8, "八月");
		DateUtil.monthMaps.put(9, "九月");
		DateUtil.monthMaps.put(10, "十月");
		DateUtil.monthMaps.put(11, "十一月");
		DateUtil.monthMaps.put(12, "十二月");

		DateUtil.weekdayMaps.put(1, "星期一");
		DateUtil.weekdayMaps.put(2, "星期二");
		DateUtil.weekdayMaps.put(3, "星期三");
		DateUtil.weekdayMaps.put(4, "星期四");
		DateUtil.weekdayMaps.put(5, "星期五");
		DateUtil.weekdayMaps.put(6, "星期六");
		DateUtil.weekdayMaps.put(0, "星期日");
	}

	public static Date paras() {
		Calendar time = Calendar.getInstance();
		// time.clear();
		int year = time.get(Calendar.YEAR);
		// 注意,Calendar对象默认一月为0
		int month = time.get(Calendar.MONTH);

		int days = time.get(Calendar.DATE);
		int hour = time.get(Calendar.HOUR);
		int mins = time.get(Calendar.MINUTE);
		int sec = time.get(Calendar.SECOND);
		//int day = time.getActualMaximum(Calendar.DAY_OF_MONTH);// 本月份的天数
		String d = year + "-" + (month + 1) + "-" + days + " " + hour + ":"
				+ mins + ":" + sec;
		System.out.println(year + "-" + month + "-" + days + " " + hour + ":"
				+ mins + ":" + sec);
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy-MM-dd hh:mm:ss");
		Date date;
		try {
			date = sdf.parse(d);
			System.out.println(date);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 通过年月日构造时间
	 */
	public static Date parseDate(int year, int month, int day) {
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(year, month - 1, day);
		return c.getTime();
	}

	/**
	 * 通过年月日时分秒构造时间
	 */
	public static Date parseDate(int year, int month, int day, int hour,
			int min, int sec) {
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(year, month - 1, day, hour, min, sec);
		return c.getTime();
	}

	/**
	 * 字符串转时间
	 * 
	 * @param text
	 *            字符串
	 * @param pattern
	 *            格式
	 * @return 时间
	 */
	public static Date parseDate(String text, String pattern) {
		try {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			return df.parse(text);
		} catch (ParseException e) {
			throw new RuntimeException("字符串转换时间失败" + text, e);
		}
	}

	/**
	 * 静默转换,无法转换返回null
	 * 
	 * @param text
	 *            字符串
	 * @return 时间
	 */
	public static Date parseDate(String text) {
		Iterator<String> it = dateFormats.iterator();
		SimpleDateFormat df = new SimpleDateFormat();
		Date date = null;
		while (it.hasNext()) {
			try {
				String pattern = (String) it.next();
				df.applyPattern(pattern);
				date = df.parse(text);
				break;
			} catch (ParseException ex) {

			}
		}
		return date;
	}

	/**
	 * 格式化日期时间
	 * 
	 * @param dt
	 *            时间
	 * @param pattern
	 *            格式
	 * @return 字符串
	 */
	public static String formatDate(Date dt, String pattern) {
		if (dt != null) {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			return df.format(dt);
		}
		return null;
	}

	/**
	 * 格式化日期时间 ,仅日期
	 * 
	 * @param dt
	 *            时间
	 * @return 字符串
	 */
	public static String formatDate(Date dt) {
		return formatDate(dt, "yyyy-MM-dd");
	}

	/**
	 * 格式化日期时间 ,年月日时分
	 * 
	 * @param dt
	 *            时间
	 * @return 字符串
	 */
	public static String formatDateTime(Date dt) {
		return formatDate(dt, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 增加指定年数
	 * 
	 * @param dt
	 *            时间
	 * @param i
	 *            年数
	 * @return 时间
	 */
	public static Date addYears(Date dt, int i) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		c.add(Calendar.YEAR, i);
		return c.getTime();
	}

	/**
	 * 增加指定月数
	 * 
	 * @param dt
	 *            时间
	 * @param i
	 *            月数
	 * @return 时间
	 */
	public static Date addMonths(Date dt, int i) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		c.add(Calendar.MONTH, i);
		return c.getTime();
	}

	/**
	 * 增加指定天数
	 * 
	 * @param dt
	 *            时间
	 * @param i
	 *            天数
	 * @return 时间
	 */
	public static Date addDays(Date dt, int i) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		c.add(Calendar.DATE, i);
		return c.getTime();
	}

	/**
	 * 增加指定小时
	 */
	public static Date addHours(Date dt, int i) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		c.add(Calendar.HOUR, i);
		return c.getTime();
	}

	/**
	 * 增加指定分钟
	 */
	public static Date addMinutes(Date dt, int i) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		c.add(Calendar.MINUTE, i);
		return c.getTime();
	}

	/**
	 * 增加指定秒
	 */
	public static Date addSeconds(Date dt, int i) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		c.add(Calendar.SECOND, i);
		return c.getTime();
	}

	/**
	 * 获取年份
	 * 
	 * @return 年
	 */
	public static int getYear(Date dt) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		return c.get(Calendar.YEAR);
	}

	/**
	 * 获取月份
	 * 
	 * @return 月
	 */
	public static int getMonth(Date dt) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		return c.get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取月份中文
	 * 
	 * @return 如:一月
	 */
	public static String getMonthText(Date dt) {
		return monthMaps.get(getMonth(dt));
	}

	/**
	 * 获取日
	 * 
	 * @return
	 */
	public static int getDay(Date dt) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		return c.get(Calendar.DATE);
	}

	/**
	 * 获取星期几
	 * 
	 * @return 0表示星期天
	 */
	public static int getDayOfWeek(Date dt) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		return c.get(Calendar.DAY_OF_WEEK) - 1;
	}

	/**
	 * 获取星期几
	 * 
	 * @return 如:星期一
	 */
	public static String getDayOfWeekText(Date dt) {
		return weekdayMaps.get(getDayOfWeek(dt));
	}

	/**
	 * 获取月份
	 * 
	 * @return 月份
	 */
	public static int getDayOfMonth(Date dt) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		return c.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取一年第几天
	 */
	public static int getDayOfYear(Date dt) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		return c.get(Calendar.DAY_OF_YEAR);
	}

	/**
	 * 获取小时
	 * 
	 * @return 小时
	 */
	public static int getHour(Date dt) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		return c.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 获取分钟
	 */
	public static int getMinute(Date dt) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		return c.get(Calendar.MINUTE);
	}

	/**
	 * 获取秒
	 */
	public static int getSecond(Date dt) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		return c.get(Calendar.SECOND);
	}

	/**
	 * 获取季度 1,2,3,4
	 */
	public static int getSeasonOfYear(Date dt) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		return (c.get(Calendar.MONTH) / 3) + 1;
	}

	/**
	 * 获取旬 1,2,3
	 */
	public static int getTenDayOfMonth(Date dt) {
		int x = (getDay(dt) - 1) / 10;
		return x < 3 ? (x + 1) : 3;
	}

	/**
	 * 是否闰年
	 */
	public static boolean isLeapYear(Date dt) {
		int year = getYear(dt);
		return isLeapYear(year);
	}

	/**
	 * 是否闰年
	 */
	public static boolean isLeapYear(int year) {
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取某年某季度第一天
	 * 
	 * @param year
	 *            年
	 * @param season
	 *            季度
	 * @return 日期
	 */
	public static Date getSeasonStart(int year, int season) {
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(year, (season - 1) * 3, 1);
		return c.getTime();
	}

	/**
	 * 获取某年某季度最后一天
	 * 
	 * @param year
	 *            年
	 * @param season
	 *            季度
	 * @return 日期
	 */
	public static Date getSeasonEnd(int year, int season) {
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(year, season * 3, 1);
		c.add(Calendar.SECOND, -1);
		return c.getTime();
	}

	/**
	 * 获取某年某月某旬的第一天
	 * 
	 * @param year
	 *            年
	 * @param month
	 *            月
	 * @param tenday
	 *            旬
	 * @return 日期
	 */
	public static Date getTenDayStart(int year, int month, int tenday) {
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(year, month - 1, (tenday - 1) * 10 + 1);
		return c.getTime();
	}

	/**
	 * 获取某年某月某旬的最后一天
	 * 
	 * @param year
	 *            年
	 * @param month
	 *            月
	 * @param tenday
	 *            旬
	 * @return 日期
	 */
	public static Date getTenDayEnd(int year, int month, int tenday) {
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(year, month - 1, 1);

		if (tenday < 3) {
			c.set(year, month - 1, tenday * 10, 23, 59, 59);
		} else {
			c.add(Calendar.MONTH, 1);
			c.add(Calendar.SECOND, -1);
		}
		return c.getTime();
	}

	/**
	 * 获取某年某月的第一天
	 * 
	 * @param year
	 *            年
	 * @param month
	 *            月
	 * @return 日期
	 */
	public static Date getMonthStart(int year, int month) {
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(year, month - 1, 1);
		return c.getTime();
	}

	/**
	 * 获取某年某月的最后一天
	 * 
	 * @param year
	 *            年
	 * @param month
	 *            月
	 * @return 日期
	 */
	public static Date getMonthEnd(int year, int month) {
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(year, month - 1, 1);
		c.add(Calendar.MONTH, 1);
		c.add(Calendar.SECOND, -1);
		return c.getTime();
	}

	/**
	 * 获取两个日期相差天数
	 * 
	 * @param startdate
	 *            起始
	 * @param enddate
	 *            结束
	 * @return 天数
	 */
	public static int getBetweenDays(Date startdate, Date enddate) {
		return (int) Math.rint((double) (enddate.getTime() - startdate
				.getTime()) / (1000 * 60 * 60 * 24));
	}

	/**
	 * Timestamp
	 */
	public static Timestamp toTimestamp(Date dt) {
		return new Timestamp(dt.getTime());
	}

	/**
	 * 现在
	 */
	public static Date now() {
		return new Date();
	}

	/**
	 * 今天凌晨0点
	 */
	public static Date today() {
		Date dt = new Date();
		return parseDate(getYear(dt), getMonth(dt), getDay(dt));
	}
	
	/**
	 *两个 时间相差
	 *@param str1 时间参数 1 格式：1990-01-01 12:00:00 
     * @param str2 时间参数 2 格式：2009-01-01 12:00:00
     * @param format 格式 day,hour,min,sec
	 * **/
	public static Double getDistanceTime(String str1,String str2,String format){
		   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        Date one;  
	        Date two;
	        try {  
	            one = df.parse(str1);  
	            two = df.parse(str2);  
	            long time1 = one.getTime();  
	            long time2 = two.getTime();  
	            double diff ;  
	            if(time1<time2) {  
	                diff = time2 - time1;  
	            } else {  
	                diff = time1 - time2;  
	            }  
	            if(format=="day"){
	            	return diff / (24 * 60 * 60 * 1000);  
	            }else if (format=="hour") {
					return diff / (60 * 60 * 1000);
				}else if (format=="min") {
					return diff / (60 * 1000);
				}else if (format=="sec") {
					return diff/1000;
				}
	            return null;
	        } catch (ParseException e) {  
	            e.printStackTrace();  
	            return null;
	        }  
	}
	/**
	 * 日期向后推多少分钟
	*
	* @author: rucheng
	* @createTime: 2018年8月7日 上午11:05:20
	* @history:
	* @param timeStr
	* @param day
	* @return String
	 */
	public static String addMinuteTime(String timeStr, int minute) {
		String startDateTime = null;
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date date = sf.parse(timeStr);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			// 当前时间向前推hour小时
			calendar.add(calendar.MINUTE, minute);
			Date endDate = calendar.getTime();
			startDateTime = sf.format(endDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return startDateTime;
	}
	
	public static String addDayTime(String timeStr, int day) {
		String startDateTime = null;
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date date = sf.parse(timeStr);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			// 当前时间向前推hour小时
			calendar.add(calendar.DATE, day);
			Date endDate = calendar.getTime();
			startDateTime = sf.format(endDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return startDateTime;
	}
	/**
	 * 日期向后推多少天
	*
	* @author: rucheng
	* @createTime: 2018年8月7日 上午11:05:20
	* @history:
	* @param timeStr
	* @param day
	* @return String
	 */
	public static String addDayTime(Date date, int day) {
		String startDateTime = null;
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd hh:mi:ss");
			GregorianCalendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			// 当前时间向前推hour小时
			calendar.add(calendar.DATE, day);
			Date endDate = calendar.getTime();
			startDateTime = sf.format(endDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return startDateTime;
	}
	public static String formatDateStr(String dateStr) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
		Date d = sdf.parse(dateStr);
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
		
	}
}
